//by k1 2022.2.19

server_SavedParams = profileNamespace getVariable ["A3E_SavedParams",[]];
publicVariable "server_SavedParams";

Sacks_blindage_class = 'Land_SandbagBarricade_01_half_F'; //
publicVariable "Sacks_blindage_class"; 

Escape_PlayersEnemy = {
	//params ["_ply", "_name", "_side"];
	{if(isNull _x || {!isPlayer _x})then{Escape_Players_enemy = Escape_Players_enemy - [_x]}} forEach Escape_Players_enemy;
	if (_this in Escape_Players_enemy) exitWith {};
	Escape_Players_enemy pushback _this;
	publicVariable "Escape_Players_enemy";
};
"Escape_ply_enemy" addPublicVariableEventHandler {(_this select 1) call Escape_PlayersEnemy};

"Escape_MutinyEvacMak" addPublicVariableEventHandler {
	if (getMarkerColor "Escape_Mutiny_evacMak" == "") then {
		_axis = worldSize / 2;
		_center = [_axis, _axis , 0];
		_radius = sqrt 2 / 2 * worldSize;
		_make = createMarker ["Escape_Mutiny_evacMak", _center];
		_make setMarkerShape "ELLIPSE"; //"rectangle"
		_make setMarkerSize [_radius, _radius];
		_make setMarkerColor "ColorRed";
		_make setMarkerBrush "Border";
	};
};

Escape_greenCircle_reduce = 200;
publicVariable "Escape_greenCircle_reduce";
Escape_greenCircle_sv = {
	if (isNil "Escape_ply_greenCircle") exitWith {};
	_t1 = 0;
	_t2 = time + 60;
	waitUntil {
		if (isNil "Escape_ply_greenCircle") exitWith {true};
		if (time > _t1) then {
			_out = round (_t2 - time);
			missionNamespace setVariable ["Escape_greenCircle_time", abs _out, true];
			if (_out <= 0) then {
				if (Escape_greenCircle_radius > 100) then {
					_radius = Escape_greenCircle_pos call Escape_fnc_plyDisMax;
					Escape_greenCircle_radius = if(Escape_greenCircle_radius - _radius > 800)then[{_radius},{Escape_greenCircle_radius - Escape_greenCircle_reduce}];
					if (Escape_greenCircle_radius < 100) then {Escape_greenCircle_radius = 100};
					publicVariable "Escape_greenCircle_radius";
				};
				remoteExec ["Escape_greenCircle_Kill",0];
				_t2 = time + 60;
			};
			_t1 = time + 1;
		};
		false
	};
};
"Escape_ply_greenCircle" addPublicVariableEventHandler {0 spawn Escape_greenCircle_sv};

Escape_fnc_plyDisMax2 = {
	private ["_disMax","_plys","_dis"];
	_disMax = 0;
	_plys = call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy;
	{
		_dis = _x distance2D _this;
		if (_dis > _disMax) then {_disMax = round _dis};
	} forEach _plys;
	_disMax
};
Escape_greenCircle2_sv = {
	params ["_pos","_dis"];
	Escape_ShrinkRing_end = true;
	sleep 3; //
	Escape_ShrinkRing_end = false;
	Escape_greenCircle2_dis = _dis;
	publicVariable "Escape_greenCircle2_dis";
	Escape_greenCircle2_radius = _pos call Escape_fnc_plyDisMax2;
	publicVariable "Escape_greenCircle2_radius";
	_mak = "Escape_greenCircle2_mak";
	if (getMarkerColor _mak == "") then {
		_mak = createMarker [_mak, _pos];
		_mak setMarkerBrush "SOLID";
		_mak setMarkerShape "ELLIPSE";
		_mak setMarkerColor "ColorGreen";
		_mak setMarkerSize [Escape_greenCircle2_radius, Escape_greenCircle2_radius];
	};
	_t1 = 0;
	_t2 = time + 60;
	_exit = false;
	waitUntil {
		if (time > _t1) then {
			_plys = call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy;
			if ({_x distance2D markerPos _mak < _dis} count _plys == count _plys || {Escape_ShrinkRing_end} || {Escape_ShrinkRing == 0}) exitWith {
				deleteMarker _mak;
				_exit = true;
				if (count _plys == 0) then {
					_this spawn {
						_check = false;
						_t = time + 1;
						waitUntil {if (time > _t) then {_check = (count (call A3E_fnc_GetPlayers - [Player_Server] - Escape_Players_enemy) > 0); _t = time + 1}; _check};
						_t = time + Escape_respawn_time + 180;
						waitUntil {time > _t || {Escape_ShrinkRing_end} || {Escape_ShrinkRing == 0}};
						if (!Escape_ShrinkRing_end && {Escape_ShrinkRing != 0}) then {_this call Escape_greenCircle2_sv};
					};
				}; ////
			};
			_out = round (_t2 - time);
			missionNamespace setVariable ["Escape_greenCircle2_time", abs _out, true];
			if (_out <= 0) then {
				remoteExec ["Escape_greenCircle2_Kill",0];
				sleep .1;
				if (Escape_greenCircle2_radius > _dis) then {
					_radius = (markerPos _mak) call Escape_fnc_plyDisMax2;
					Escape_greenCircle2_radius = if(Escape_greenCircle2_radius - _radius > 800)then[{_radius},{Escape_greenCircle2_radius - 200}];
					if (Escape_greenCircle2_radius < _dis) then {Escape_greenCircle2_radius = _dis};
					_mak setMarkerSize [Escape_greenCircle2_radius, Escape_greenCircle2_radius];
					publicVariable "Escape_greenCircle2_radius";
				};
				_t2 = time + 60;
			};
			if (!isNil "Escape_ply_greenCircle") then {Escape_ply_greenCircle = nil; publicVariable "Escape_ply_greenCircle"};
			_t1 = time + 1;
		};
		_exit
	};
};

waitUntil {!isNil "A3E_ParamsParsed"};
0 spawn {
	_t = time + 300;
	waitUntil {(!isNil "A3E_Task_Prison_Complete" && {A3E_Task_Prison_Complete}) || {time > _t}};
	waitUntil {!isNil "Escape_intelMaks" && {!isNil "Escape_Players_enemy"}};
	if (count Escape_intelMaks > 0) then {
		_sel = selectRandom Escape_intelMaks;
		_sel params ["_pos", "_mak"];
		_houses = nearestObjects [_pos, ["house"], Escape_intel_Range]; //300
		_pos = [(_pos select 0)-random 5 +random 5,(_pos select 1)-random 5 +random 5,_pos select 2];
		if (count _houses > 0) then {
			_t = time + 5;
			_pos = while {true} do {
				_Bulding = selectRandom _houses;
				_Rooms = _Bulding buildingPos -1;
				_roomPos = selectRandom _Rooms;
				if (!isNil "_roomPos") exitWith {_roomPos};
				if (time > _t) exitWith {_pos};
			};
		};
		Escape_intel_box = createVehicle ["Land_Suitcase_F", _pos, [], 0, "CAN_COLLIDE"];
		publicVariable "Escape_intel_box";
		Escape_intelBox_originalPos = _pos;
		publicVariable "Escape_intelBox_originalPos";
	};
	Escape_intel_t2 = 0;
	Escape_intel_ttt = 0;
	Escape_intel_range2 = Escape_intel_Range - 10;
	["Escape_intel_loop", "onEachFrame", {
	 if (isNil "Escape_intel_upload") then [{
	  if (time > Escape_intel_ttt) then {
		if (!isNull Escape_intel_box) then [{
			_obj = attachedTo Escape_intel_box;
			if (isNull _obj) then [{
				if (getPosASL Escape_intel_box select 2 < -2 || {getPosATL Escape_intel_box select 2 < -2}) then {
					Escape_intel_box setPos [getPosASL Escape_intel_box select 0,getPosASL Escape_intel_box select 1,0];
					//sleep .1;
				}; //地底，水里
			},{
				if (!alive _obj || {lifeState _obj == "INCAPACITATED"} || {_obj getVariable ["FAR_isUnconscious", 0] == 1}) then {detach Escape_intel_box}; //自动放下
				if (_obj isKindOf "CaManBase" && {!isNull objectParent _obj}) then {
					if (vehicle _obj isKindOf "StaticWeapon") then [{detach Escape_intel_box},{Escape_intel_box attachTo [vehicle _obj, [0,-1,-.2]]}];
				}; //绑到载具
			}];
			if (!isNil "Escape_intel_find") then {
				if (!isNull _obj && {{_obj == vehicle _x} count (call A3E_fnc_GetPlayers - Escape_Players_enemy) > 0}) then [{
					if (missionNamespace getVariable ["Escape_intelAllowTake_time", Escape_intelTake_time] != Escape_intelTake_time) then {
						Escape_intelTakeTime_out = nil;
						missionNamespace setVariable ["Escape_intelAllowTake_time", Escape_intelTake_time,true];
						if (getMarkerColor "Escape_intel_mak2" != "") then {"Escape_intel_mak2" setMarkerText format["情报所在区域 (%1s)", Escape_intelTake_time]};
					};
				},{
					if (missionNamespace getVariable ["Escape_intelAllowTake_time", Escape_intelTake_time] > 0) then {
						if (isNil "Escape_intelTakeTime_out") then {Escape_intelTakeTime_out = time + Escape_intelTake_time};
						_out = round (Escape_intelTakeTime_out - time);
						if (_out < 0) then {_out = 0};
						missionNamespace setVariable ["Escape_intelAllowTake_time", _out,true];
						if (getMarkerColor "Escape_intel_mak2" != "") then {"Escape_intel_mak2" setMarkerText format["情报所在区域 (%1s)", _out]};
						if (!isNull _obj && {{_obj == vehicle _x} count Escape_Players_enemy > 0}) then {detach Escape_intel_box};
					};
				}];
			};
			Escape_intel_box_Pos = getPosASL Escape_intel_box;
			if (getMarkerColor "Escape_intel_mak" != "" && {!(Escape_intel_box_Pos inArea "Escape_intel_mak")}) then {
				_pos = Escape_intel_box getPos [random Escape_intel_range2, random 360];
				"Escape_intel_mak" setMarkerPos _pos;
				"Escape_intel_mak2" setMarkerPos _pos;
			};
			if (getMarkerColor "Escape_Mutiny_evacMak" != "" && {!isNull attachedTo Escape_intel_box} && {{attachedTo Escape_intel_box == vehicle _x && {alive _x}} count Escape_Players_enemy > 0} && {!(getPosASL Escape_intel_box inArea "Escape_Mutiny_evacMak")} && {isNil "Escape_Mutiny_evacEndMission"}) then {
				Escape_Mutiny_evacEndMission = 1;
				Escape_mission_end = 1;
				publicVariable "Escape_mission_end";
				["end5",false,true] remoteExec ['BIS_fnc_endMission', 0];
			};
			if (!isNil "Escape_intel_takenAway" && {time > Escape_intel_t2}) then {
				{
					_ai = _x;
					if (alive _ai && {side _ai == A3E_VAR_Side_Opfor} && {!(vehicle _ai isKindOf "StaticWeapon")} && {!unitIsUAV vehicle _ai} && {lifeState _ai != "INCAPACITATED"} && {{_ai distance _x < 150} count A3E_Var_ClearedPositions == 0}) then {
						_ai allowFleeing 0;
						_ai setBehaviour (if(isNull(_ai findNearestEnemy _ai))then[{"CARELESS"},{"AWARE"}]);
						_doMovePos = if(surfaceIsWater getPosASL Escape_intel_box)then[{getPosASL Escape_intel_box},{getPosATL Escape_intel_box}];
						if (vehicle _ai isKindOf "LandVehicle" || {vehicle _ai isKindOf "Ship"}) then {
							_tag = if(isNull _obj || {_obj in Escape_Players_enemy})then[{Escape_intel_box},{_obj}];
							_dir = _ai getDir _tag;
							_doMovePos = _tag getPos [35, _dir];
							if (_tag == Escape_intel_box) exitWith {};
							vehicle _ai commandTarget _tag;
							vehicle _ai commandFire _tag;
						};
						_ai doMove (if(_ai distance Escape_intel_box > 30)then[{_doMovePos},{[(_doMovePos select 0) + (random [-7.5, 7.5, 0]), (_doMovePos select 1) + (random [-7.5, 7.5, 0]), _doMovePos select 2]}]);
						(group _ai) setSpeedMode "FULL";
					};
				} forEach allUnits;
				Escape_intel_t2 = time + 9;
			};
		},{
			Escape_intel_box = createVehicle ["Land_Suitcase_F", [0,0,0], [], 0, "CAN_COLLIDE"];
			waitUntil {!isNull Escape_intel_box};
			Escape_intel_box allowDamage false;
			Escape_intel_box setPosASL Escape_intel_box_Pos;
			publicVariable "Escape_intel_box";
		}];
		{
			if (!isNil "Escape_intel_find") exitWith {};
			_x params ["_pos", "_mak"];
			if ({_x distance _pos < 30 && {side _x == A3E_VAR_Side_Blufor}} count (call A3E_fnc_GetPlayers - Escape_Players_enemy) > 0) then {
				_pos2 = getPosASL Escape_intel_box;
				if (_pos2 inArea _mak) then [{
					call Escape_create_intelMak;
				},{
					deleteMarker _mak;
					Escape_intelMaks = Escape_intelMaks - [_x];
					for "_i" from 1 to (round random Escape_intelMaks_null) do {
						if (count Escape_intelMaks == 0) exitWith {};
						_sel = selectRandom Escape_intelMaks;
						_sel params ["_pos", "_mak"];
						if !(_pos2 inArea _mak) then {deleteMarker _mak; Escape_intelMaks = Escape_intelMaks - [_sel]};
					};
					if (count Escape_intelMaks < 2) then [{call Escape_create_intelMak},{["情报位置更新"] call drn_fnc_CL_ShowTitleTextAllClients}];
				}];
			};
		} forEach Escape_intelMaks;
		Escape_intel_ttt = time + 1;
	  };
	 },{
		detach Escape_intel_box;
		Escape_intel_takenAway = nil;
		Escape_intel_box = objNull;
		publicVariable "Escape_intel_box";
		if (getMarkerColor "Escape_intel_mak" != "") then {deleteMarker "Escape_intel_mak"; deleteMarker "Escape_intel_mak2"};
		["数据上传完毕."] call drn_fnc_CL_ShowTitleTextAllClients;
		["Escape_intel_loop", "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
		0 spawn {
			{
				_ai = _x;
				if (alive _ai && {side _ai == A3E_VAR_Side_Opfor} && {!unitIsUAV vehicle _ai} && {{_ai distance _x < 150} count A3E_Var_ClearedPositions == 0}) then {
					_ai doMove getPosATL _ai;
					_ai doFollow leader _ai;
				};
			} forEach allUnits;
		}; //
	 }];
	}] call BIS_fnc_addStackedEventHandler;
};

Escape_getVar1 = {
	Escape_StrongholdPosArr = A3E_Var_ClearedPositions + Escape_RoadblocksPos;
	remoteExecutedOwner publicVariableClient "Escape_StrongholdPosArr";
};

Escape_fn_killAI = {
	private ["_Players","_c","_posArr"];
	_c = 0;
	_Players = allPlayers - [entities "HeadlessClient_F"];
	_posArr = A3E_Var_ClearedPositions + Escape_RoadblocksPos;
	params ["_dis1", "_dis2"];
	{
		_ai = _x;
		if (alive _ai && {!isPlayer _ai} && {_ai isKindOf "CaManBase"} && {!(vehicle _x isKindOf "air")} && {!(vehicle _x isKindOf "Ship")} && {{_ai distance _x < _dis1} count _Players == 0} && {{_ai distance _x < _dis2} count _posArr == 0}) then {vehicle _x setDamage 1; _c = _c + 1};
	} forEach allUnits;
	if (_c > 0) then {format["清理%1米外AI: %2个", _dis1,_c] remoteExec ["systemchat", 0]};
};

kk_getObjSize = {
	private ["_bbr","_p1","_p2","_maxWidth","_maxLength","_maxHeight"];
	_bbr = boundingBoxReal _this;
	_p1 = _bbr select 0;
	_p2 = _bbr select 1;
	_maxWidth = abs ((_p2 select 0) - (_p1 select 0));
	_maxLength = abs ((_p2 select 1) - (_p1 select 1));
	_maxHeight = abs ((_p2 select 2) - (_p1 select 2));
	[_maxWidth,_maxLength,_maxHeight]
};
kk_getInsObj = {
	private ["_h","_beg","_end","_ins","_insObj"];
	params ["_unit","_dis1","_dis2","_offset","_z1","_z2"];
	_h = getPosASL _unit select 2;
	_beg = _unit modelToWorld [_offset,_dis1,0];
	_end = _unit modelToWorld [_offset,_dis2,0];
	_beg set [2, _h + _z1];
	_end set [2, _h + _z2];
	_ins = lineIntersectsSurfaces [_beg, _end, _unit, _unit, true, 1, "FIRE", "FIRE"];
	_insObj = if(count _ins == 0)then[{objNull},{_ins select 0 select 2}];
	_insObj
};
Escape_missileTrack_loopIds = [];
Escape_fn_missileTrack = {
	params ["_obj","_tag"];
	if (!alive _obj || {isNull _tag}) exitWith {};
	_i = 0;
	_id = "Escape_missileTrack_loop_0";
	while {_id in Escape_missileTrack_loopIds} do {_i = _i + 1; _id = format["Escape_missileTrack_loop_%1",_i]};
	Escape_missileTrack_loopIds pushback _id;
	[_id, "onEachFrame", {
		params ["_obj","_tag","_id"];
		if (alive _obj) then [{
			_pos0 = getPosASL _obj; 
			_pos1 = getPosASL _tag; 
			_pos1 set [2, (_pos1 select 2) - (if(_obj isKindOf "Helicopter")then[{10},{35}])];
			_dir0 = _pos0 vectorFromTo _pos1; 
			_dir1 = _pos1 vectorFromTo _pos0; 
			_obj setVelocityTransformation [_pos0,_pos1,velocity _obj,velocity _obj,_dir0,_dir1,[0,0,1],[0,0,1],.005]; 
		},{
			[_id, "onEachFrame"] call BIS_fnc_removeStackedEventHandler;
			Escape_missileTrack_loopIds = Escape_missileTrack_loopIds - [_id];
		}];
	},[_obj,_tag,_id]] call BIS_fnc_addStackedEventHandler;
};
Escape_uavKiller_forceMove = {
	params ["_uav","_tag"];
	_uav setVariable ["Escape_uavKiller_speedSimulation", 1];
	_t1 = 0;
	_t2 = 0;
	_pos1 = [0,0,0];
	_maxVehSpeed = ((getNumber (configFile >> "CfgVehicles" >> typeOf _uav >> "maxSpeed")) * 1000) / 3600;
	_speed = _maxVehSpeed/10;
	while {alive _uav && {_uav distance2D _tag < 30} && {_uav distance2D _tag > 9} && {!isNil {_uav getVariable "Escape_uavKiller_speedSimulation"}} && {canMove _uav} && {fuel _uav > 0} && {!(_uav call Escape_uavKiller_checkCond5)}} do {
		if (time > _t1) then {
			if (time > _t2) then {_speed = if(getPosASL _uav distance _pos1 > _maxVehSpeed)then [{_speed - .1},{_speed + .1}];_pos1 = getPosASL _uav; _t2 = time + 1};
			_dir = _uav getDir _tag;  
			_pos = _uav getPos [_speed, _dir];
			if (getPos _uav select 2 < .2) then {_pos set [2, getPosATL _uav select 2]};
			_uav setDir _dir;
			_uav setVehiclePosition [_pos, [], 0, "CAN_COLLIDE"];
			_t1 = time + .1;
		};
		sleep .001;
	};
	_uav setVariable ["Escape_uavKiller_speedSimulation", nil];
};
Escape_uavKiller_code1 = {
	waitUntil {sleep 3; _enemy = _uav findNearestEnemy _uav; _tag = if(isPlayer _enemy && {({alive _x && {_x getVariable ["FAR_isUnconscious", 0] == 0}} count crew _enemy) > 0})then[{[_uav,_enemy] call Escape_minDisVeh},{objNull}]; (alive _tag || {!alive _uav})};
	_uav reveal _tag;
};
Escape_uavKiller_code5 = {
	_posASL = getPosASL _tag;
	_h = if(surfaceIsWater _posASL)then[{_posASL select 2},{getPosATL _tag select 2}];
	_uav flyInHeight (_h + 50);
};
Escape_uavKiller_checkCond1 = {
	_tag2 = _uav findNearestEnemy _uav;
	(isNull _tag2 || {_tag != _tag2 && {isPlayer _tag2} && {({alive _x && {_x getVariable ["FAR_isUnconscious", 0] == 0}} count crew _tag2) > 0} && {_tag distance _tag2 > 350} && {_uav distance _tag > _uav distance _tag2}})
};
Escape_uavKiller_checkCond5 = {
	private ["_bbr","_maxWidth","_maxLength","_maxHeight","_width","_height","_obstacle","_scanning","_obj","_area"];
	_bbr = _this call kk_getObjSize;
	_maxWidth = _bbr select 0;
	_maxLength = _bbr select 1;
	_maxHeight = _bbr select 2;
	_area = _maxWidth*_maxLength;
	_maxWidth = _maxWidth/2;
	_maxLength = _maxLength/2 + 2;
	if (_maxHeight >= 2) then {_maxHeight = _maxHeight - 1.5};
	_obstacle = false;
	_width = -_maxWidth;
	_height = 0;
	_scanning = true;
	while {_scanning} do {
		_obj = [_this,0,_maxLength,_width,_height,_height] call kk_getInsObj;
		if (!isNull _obj && {!(_obj isKindOf "man")} && {call {(_obj call kk_getObjSize) params ["_maxWidth","_maxLength"]; (_maxWidth*_maxLength) > _area/8}}) exitWith {_obstacle = true};
		if (_width < _maxWidth) then [{_width = _width + .1},{if (_height < _maxHeight) then [{_height = _height + .2},{_scanning = false}]; _width = -_maxWidth}];
	};
	_obstacle
};
Escape_fn_UAVSelfExplosion = {
	_uav = _this;
	_uav setVariable ["Escape_uavKiller_addC4", false];
	if (!(side _uav in [A3E_VAR_Side_Opfor,A3E_VAR_Side_Ind]) || {!alive driver _uav} || {{_uav distance _x < 3} count allPlayers > 0} || {isUAVConnected _uav} || {count (attachedObjects _uav - [objNull]) > 0}) exitWith {};
	_t = time + 15;
	_grp = group _uav;
	if (!local _grp) then {_grp setGroupOwner 2; waitUntil {sleep 1; (local _grp || {time > _t})}};
	if (time > _t) exitWith {};
	_crew = crew _uav;
	{[_x,.8] call Escape_setSkills; _x enableAI "all"; _x doFollow _x;} forEach _crew;
	_exp = "SatchelCharge_Remote_Ammo" createVehicle [0,0,0];
	_z = if(_uav isKindOf "Helicopter")then[{.15},{-.7}];
	_exp attachTo [_uav, [0,0,_z]];
	_isAir = _uav isKindOf "air";
	if (getPos _uav select 2 < .5 && {_isAir}) then {_uav engineOn true; _uav doMove (_uav getPos [150, getDir _uav])};
	_tag = objNull;
	call Escape_uavKiller_code1;
	if (!alive _uav) exitWith {_exp setDamage 1};
	if (_isAir) then {
		call Escape_uavKiller_code5;
		_t = time + 20;
		_dis = _uav distance2D _tag;
		_dis = if(_dis < 100)then[{_dis*.4},{40}];
		waitUntil {sleep 3; getPos _uav select 2 > _dis || {time > _t} || {!alive _uav}};
	};
	if (!alive _uav) exitWith {_exp setDamage 1};
	_t1 = 0;
	_dis = 100;
	_smoker = objNull;
	_z = if(_uav isKindOf "Helicopter")then[{0},{_dis = 200; -.7}];
	_grp setSpeedMode "FULL";
	if (!_isAir) then {_uav setVariable ["Escape_uavKiller_speedSimulation", nil]};
	while {alive _uav} do {
		if (!alive _tag || {({alive _x && {_x getVariable ["FAR_isUnconscious", 0] == 0}} count crew _tag) == 0} || {call Escape_uavKiller_checkCond1}) then {_uav doMove position _uav; _uav doFollow _uav; if (!_isAir) then {_uav setVariable ["Escape_uavKiller_speedSimulation", nil]}; call Escape_uavKiller_code1};
		if (alive _uav) then {
			if (time > _t1) then {
				_uav enableAI "all";
				_grp allowFleeing 0;
				_pos = position _tag;
				_uav doMove _pos;
				_uav moveTo _pos;
				if (_isAir) then {call Escape_uavKiller_code5};
				_t1 = time + 5;
			};
			if (Escape_uavKiller_smoker == 1 && {isNull _smoker} && {_uav distance _tag < 500}) then {_smoker = "SmokeShellRed" createVehicle [0,0,0]; _smoker attachTo [_uav, [0,0,_z]]};
		};
		_dis2 = _uav distance2D _tag;
		if (_dis2 < _dis && {_isAir}) exitWith {
			if (alive _uav && {getPos _uav select 2 > .5} && {isEngineOn _uav}) then {[_uav,_tag] spawn Escape_fn_missileTrack};
			_t = time + 60;
			waitUntil {!alive _uav || {_uav distance _tag < 6} || {getPos _uav select 2 < -.1} || {time > _t}};
			_exp setDamage 1;
			_uav setDamage 1;
		};
		if (alive _uav && {_dis2 < 30} && {_dis2 > 9} && {!_isAir} && {isNil {_uav getVariable "Escape_uavKiller_speedSimulation"}} && {canMove _uav} && {fuel _uav > 0} && {!(_uav call Escape_uavKiller_checkCond5)}) then {[_uav,_tag] spawn Escape_uavKiller_forceMove};
		_t2 = time + .1;
		waitUntil {time > _t2 || {!alive _uav}};
		if (!alive _uav || {!canMove _uav} || {fuel _uav == 0} || {_uav distance _tag < 10 && {!_isAir}}) exitWith {_exp setDamage 1; _uav setDamage 1};
	};
};

if (isNil "Escape_uavKiller_exclusionListPack") then {Escape_uavKiller_exclusionListPack = []};
if (isNil "Escape_uavKiller_exclusionListUAV") then {Escape_uavKiller_exclusionListUAV = ["B_T_UAV_03_dynamicLoadout_F"]};
if (isNil "Escape_uavKiller_includeListUAV") then {Escape_uavKiller_includeListUAV = []};
0 spawn {
	sleep 10;
	waitUntil {!isNil "Escape_all_assets" && {!isNil "Escape_Players_enemy"}};
	Escape_Universal_c1 = 0;
	Escape_Universal_ttt = 0;
	Escape_Universal_t1 = time + 900;
	Escape_Universal_t2 = time + 300;
	Escape_Universal_t3 = time + 300;
	Escape_Universal_t4 = time + 300;
	["Escape_Universal_loop", "onEachFrame", {
	 if (time > Escape_Universal_ttt) then {
		{
			_obj = _x;
			if (alive _obj) then [{
				if ({_x distance _obj < 30} count (call A3E_fnc_GetPlayers - Escape_Players_enemy) > 0) then {
					_obj setVariable ["Escape_assets_control", nil, true];
					Escape_all_assets = Escape_all_assets - [_obj];
				};
			},{
				Escape_all_assets = Escape_all_assets - [_obj];
			}];
			
		} forEach Escape_all_assets;
		if (count Escape_all_assets > 0) then {publicVariable "Escape_all_assets"};
		
		if (Escape_TimeMultiplier_night == 0) then [{
			if (isNil "Escape_set_TimeMultiplier") then {Escape_set_TimeMultiplier = 1; setTimeMultiplier Param_TimeMultiplier};
		},{
			_hr = date select 3;
			if (_hr > 4 && {_hr < 20}) then [{if(isNil "Escape_set_TimeMultiplier")then{Escape_set_TimeMultiplier = 1; setTimeMultiplier Param_TimeMultiplier}},{if(!isNil "Escape_set_TimeMultiplier")then{Escape_set_TimeMultiplier = nil; setTimeMultiplier Param_TimeMultiplier2}}];
		}];
		
		if (time > Escape_Universal_t4) then {
			if (({alive _x && {_x isKindOf "CaManBase"} && {!isPlayer _x}} count allUnits) > Escape_AI_numMAX) then {
				[500,150] spawn Escape_fn_killAI;
				Escape_Universal_t1 = time + 900;
				Escape_Universal_c1 = 0;
			};
			Escape_Universal_t4 = time + 300;
		};
		/*if (diag_fps < 15) then [{
			if (Escape_Universal_c1 > 2) then [{[500,150] spawn Escape_fn_killAI; Escape_Universal_t1 = time + 900; Escape_Universal_c1 = 0},{Escape_Universal_c1 = Escape_Universal_c1 + 1}];
		},{
			if (Escape_Universal_c1 > 0) then {Escape_Universal_c1 = 0};
		}]; //4*/
		if (time > Escape_Universal_t1) then {
			[2000,300] spawn Escape_fn_killAI;
			Escape_Universal_t1 = time + 900;
			Escape_Universal_c1 = 0;
		};
		
		if (Escape_uavKiller == 1 && {isNil "kk_uavExp"}) then {
			if (time > Escape_Universal_t2) then {
				{
					_ai = _x;
					if (alive _ai && {backpack _ai find "_UAV_" != -1} && {!(backpack _ai in Escape_uavKiller_exclusionListPack)} && {local _ai} && {simulationEnabled _ai} && {isNull objectParent _ai} && {lifeState _ai != "INCAPACITATED"} && {!(animationState _ai in ["unconscious","unconsciousoutprone"])} && {!surfaceIsWater getPosASL _ai || {getPos _ai select 2 > -.06}} && {!isPlayer _ai}) then {
						_tag = _ai findNearestEnemy _ai;
						if (isPlayer _tag && {({alive _x && {_x getVariable ["FAR_isUnconscious", 0] == 0}} count crew _tag) > 0} && {_ai distance _tag > 35} && {_ai distance _tag < 2000} && {random 99 < Escape_uavKiller_probability}) then {
							_obstacle = false;
							{_obj = _x call kk_getInsObj; if (!isNull _obj && {!(_obj isKindOf "man")}) exitWith {_obstacle = true}} forEach [[_ai,0,1.9,0,0,0],[_ai,0,1.9,0,.2,.2],[_ai,0,1.9,0,.5,.5],[_ai,0,1.7,.3,.5,.5],[_ai,0,1.7,-.3,.5,.5],[_ai,0,1.7,.6,.7,.7],[_ai,0,1.7,-.6,.7,.7],[_ai,0,1.9,0,1,1],[_ai,0,1.9,0,1.5,1.5],[_ai,0,0,0,.5,60],[_ai,1.9,1.9,0,0,60]];
							if (_obstacle) exitWith {};
							_ai spawn {
								_ai = _this;
								_class = getText (configFile >> "CfgVehicles" >> backpack _ai >> "assembleInfo" >> "assembleTo");
								if (_class isEqualTo "") exitWith {};
								doStop _ai;
								sleep .5;
								[_ai, "amovpknlmstpsraswrfldnon_ainvpknlmstpsraswrfldnon_putdown"] remoteExec ["playMoveNow", 0];
								sleep 2.5;
								if (!alive _ai || {lifeState _ai == "INCAPACITATED"} || {animationState _ai in ["unconscious","unconsciousoutprone"]}) exitWith {};
								removeBackpackGlobal _ai;
								_uav = _class createVehicle [0,0,999];
								_uav setDir (getDir _ai);
								_pos = _ai modelToWorld [0,1.5,0];
								if (surfaceIsWater _pos) then [{_uav setPosASL _pos},{_uav setPos _pos}];
								createVehicleCrew _uav;
								{[_x] joinSilent (group _ai)} forEach crew _uav;
								_uav spawn Escape_fn_UAVSelfExplosion;
								_ai doFollow _ai;
								_ai doMove (_ai getRelPos [35, [90,270] call BIS_fnc_randomInt]);
							};
						};
					};
				} forEach allUnits;
				Escape_Universal_t2 = time + (30 + random 150);
			};
			{
				if ((_x isKindOf "Helicopter" || {typeOf _x in Escape_uavKiller_includeListUAV}) && {_x getVariable ["Escape_uavKiller_addC4", true]} && {!(typeOf _x in Escape_uavKiller_exclusionListUAV)} && {alive _x}) then {_x spawn Escape_fn_UAVSelfExplosion};
			} forEach allUnitsUAV;
		};
		
		if (!isNil "kk_fog_loop" && {time > Escape_Universal_t3}) then {
			_fog = random 1;
			if(_fog < .3)then{_fog = .3};
			30 setFog _fog; //
			Escape_Universal_t3 = time + 180 + random 720;
		};
		
		Escape_Universal_ttt = time + 10;
	 };
	}] call BIS_fnc_addStackedEventHandler;
}; //通用

0 spawn {
	Escape_air_killer = [];
	DR_laser_count = 0;
	Escape_airKiller_t = 0;
	sleep 300;
	["Escape_airKiller_loop", "onEachFrame", {
		if (time > Escape_airKiller_t) then {
			if (random 99 < Escape_air_killerProbability) then {
				_vehs = [];
				{if (!(vehicle _x in _vehs) && {_x getVariable ["FAR_isUnconscious", 0] == 0} && {_x call Escape_vehWep_check2}) then {_vehs pushback vehicle _x}} forEach (call A3E_fnc_GetPlayers - Escape_Players_enemy);
				_c = count _vehs;
				if (_c > 0) then {
					{if(!alive _x)then{Escape_air_killer = Escape_air_killer - [_x]}} forEach Escape_air_killer;
					_num = if(_c == 1)then[{1},{selectRandom (if(_c == 2)then[{[1,2]},{[3,4]}])}];
					for "_i" from 1 to (_num - count Escape_air_killer) do {
						(selectRandom _vehs) spawn (selectRandom [A3E_fnc_spawnVehicle,A3E_fnc_spawnVehicle2]);
					};
				};
			};
			Escape_airKiller_t = time + (60 + random 540); //
		};
	}] call BIS_fnc_addStackedEventHandler;
};

0 spawn {
	waitUntil {count Escape_allStatics > 0};
	Escape_allStatics_t = 0;
	Escape_MGGMA = a3e_arr_Escape_SWC_MG_A + a3e_arr_Escape_SWC_GM_A;
	["Escape_allStatics_loop", "onEachFrame", {
		if (time > Escape_allStatics_t) then {
			{
				if (alive _x) then [{
					_statics = _x;
					if (Param_EnemyFrequency == 3 || {{_x distance _statics < 999} count (call A3E_fnc_GetPlayers - Escape_Players_enemy) > 0}) then {
						if (vectorUp _x select 2 < .6 || {getPos _x select 2 < -.3}) then {_x setVehiclePosition [_x,[],0,"CAN_COLLIDE"]}; //翻到扶正
						if (typeOf _x in Escape_MGGMA) then [{createVehicleCrew _x},{[_x,A3E_VAR_Side_Opfor] spawn A3E_fnc_AddStaticGunner}];
						Escape_allStatics = Escape_allStatics - [_x];
					};
				},{
					Escape_allStatics = Escape_allStatics - [_x];
				}];
			} forEach Escape_allStatics;
			Escape_allStatics_t = time + 3;
		};
	}] call BIS_fnc_addStackedEventHandler;
}; //据点固定武器加炮手

0 spawn {
	ply_jip_debug1 = true;
	publicVariable "ply_jip_debug1";
	sleep 120;
	//uiSleep 120; //
	ply_jip_debug1 = false;
	publicVariable "ply_jip_debug1";
};

kk_spawn_id = {
	private _id = _this; 
	for "_i" from 1 to 16 do {_id = _id + str round(random 9)};
	_id
};
ES_Server_id = "ES_S_" call kk_spawn_id;
publicVariable "ES_Server_id";


